/*
 * Tencent is pleased to support the open source community by making
 * MMKV available.
 *
 * Copyright (C) 2018 THL A29 Limited, a Tencent company.
 * All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *       https://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tencent.mmkv;


import ohos.rpc.MessageParcel;
import ohos.rpc.ReliableFileDescriptor;
import ohos.system.OsHelperErrnoException;
import ohos.utils.Parcel;
import ohos.utils.Sequenceable;

import java.io.IOException;


public final class ParcelableMMKV implements Sequenceable {
    private final String mmapID;
    private int ashmemFD = -1;
    private int ashmemMetaFD = -1;
    private String cryptKey = null;

    public ParcelableMMKV(MMKV mmkv) {
        mmapID = mmkv.mmapID();
        ashmemFD = mmkv.ashmemFD();
        ashmemMetaFD = mmkv.ashmemMetaFD();
        cryptKey = mmkv.cryptKey();
    }

    private ParcelableMMKV(String id, int fd, int metaFD, String key) {
        mmapID = id;
        ashmemFD = fd;
        ashmemMetaFD = metaFD;
        cryptKey = key;
    }

    public ParcelableMMKV() {
        throw new RuntimeException("Stub!");
    }

    public MMKV toMMKV() {
        if (ashmemFD >= 0 && ashmemMetaFD >= 0) {
            return MMKV.mmkvWithAshmemFD(mmapID, ashmemFD, ashmemMetaFD, cryptKey);
        }
        return null;
    }



    @Override
    public boolean marshalling(Parcel parcel) {
        try {
            parcel.writeString(mmapID);
            ReliableFileDescriptor fd = ReliableFileDescriptor.takeChargeOfFileDescriptor(ashmemFD);
            ReliableFileDescriptor metaFD = ReliableFileDescriptor.takeChargeOfFileDescriptor(ashmemMetaFD);
            MessageParcel messageParcel= MessageParcel.create();
            messageParcel.writeFileDescriptor(fd.getFileDescriptor());
            messageParcel.writeFileDescriptor(metaFD.getFileDescriptor());
            if (cryptKey != null) {
                parcel.writeString(cryptKey);
            }
        } catch (IOException | OsHelperErrnoException e) {
            e.printStackTrace();
        }
        return false;
    }
//    public static final Sequenceable.Producer<ParcelableMMKV> CREATOR = new Sequenceable.Producer<ParcelableMMKV>() {
//        @Override
//        public ParcelableMMKV createFromParcel(Parcel source) {
//            String mmapID = source.readString();
//            ReliableFileDescriptor  fd = createFromParcel(source);
//            ParcelFileDescriptor metaFD = ParcelFileDescriptor.CREATOR.createFromParcel(source);
//            String cryptKey = source.readString();
//            if (fd != null && metaFD != null) {
//                return new ParcelableMMKV(mmapID, fd.detachFd(), metaFD.detachFd(), cryptKey);
//            }
//            return null;
//        }
//
//        @Override
//        public ParcelableMMKV[] newArray(int size) {
//            return new ParcelableMMKV[size];
//        }
//    };
    @Override
    public boolean unmarshalling(Parcel parcel) {
        return false;
    }
}
